<?php
if (!(defined('IN_IA'))) 
{
    exit('Access Denied');
}

class Category_MxShopPage extends ComWebPage {

    public function __construct($_com='virtual')
    {
        parent::__construct($_com);
    }

    function main() {

        global $_W, $_GPC;

        if (!empty($_GPC['catname'])) {

            ca('goods.virtual.category.edit');

            foreach ($_GPC['catname'] as $id => $catname) {
                $catname = trim($catname);
                if(empty($catname)){
                    continue;
                }
                if ($id == 'new') {
                    pdo_insert('mx_shop_virtual_category', array('name' => $catname, 'uniacid' => $_W['uniacid'], 'merchid' => 0));
                    $insert_id = pdo_insertid();
                    plog('goods.virtual.category.add', "添加分类 ID: {$insert_id}");
                } else {
                    pdo_update('mx_shop_virtual_category', array('name' => $catname), array('id' => $id, 'merchid' => 0));
                    plog('goods.virtual.category.edit', "修改分类 ID: {$id}");
                }
            }
            plog('goods.virtual.category.edit', '批量修改分类');
            show_json(1, array('url' => webUrl('goods/virtual/category')));
        }
        $list = pdo_fetchall("SELECT * FROM " . tablename('mx_shop_virtual_category') . " WHERE uniacid = '{$_W['uniacid']}' and merchid=0 ORDER BY id DESC");
        include $this->template();
        
    }

    function delete() {

        global $_W, $_GPC;
        $id = intval($_GPC['id']);
        $item = pdo_fetch("SELECT id,name FROM " . tablename('mx_shop_virtual_category') . " WHERE id = '$id' and merchid=0 AND uniacid=" . $_W['uniacid'] . "");
        if (empty($item)) {
            $this->message('抱歉，分类不存在或是已经被删除！', webUrl('goods/virtual/category', array('op' => 'display')), 'error');
        }
        pdo_delete('mx_shop_virtual_category', array('id' => $id));
        plog('goods.virtual.category.delete', "删除分类 ID: {$id} 标题: {$item['name']} ");

        show_json(1, array('url' => webUrl('goods/virtual/category', array('op' => 'display'))));
    }

}
